import pandas as pd

from ..extensions import db
from ..models.inventory import Inventory

def process_excel(file_path):
    """
    处理Excel文件并入库
    :param file_path: Excel文件路径
    """
    try:
        df = pd.read_excel(file_path)
        
        # 数据清洗转换
        df = df.dropna(subset=['物料编码'])
        df['数量'] = df['数量'].astype(int)
        
        # 批量入库
        with db.session.begin():
            for _, row in df.iterrows():
                item = Inventory(
                    material_type=row['类型'],
                    item_code=row['物料编码'],
                    quantity=row['数量']
                )
                db.session.add(item)
                
    except Exception as e:
        raise Exception(f"文件处理失败: {str(e)}")